#include "../lv_project_inc/lvgl_weather.h"

//初始化天气
void lvgl_weather_init(lv_obj_t *obj)
{
    fd_weather_socket = client_weather_run();
    if(fd_weather_socket == -1)
    {
        perror("client_weather_run error");
        exit(0);
    }

    printf("weatherfd %d\n",fd_weather_socket);

    char *request = "[request content]";
    
    int ret = send(fd_weather_socket,request,strlen(request),0);
    printf("weather send ret = %d\n",ret);
    
    ssize_t size_r = 1024 * 1024;
    buf_weather_local = malloc(size_r);
    memset(buf_weather_local,0,size_r);

    ret = recv(fd_weather_socket,buf_weather_local,size_r,0);
    printf("weather recv ret = %d\n",ret);

    printf("weather recv %s\n",buf_weather_local);

    char *json_start = strchr(buf_weather_local,'{');
    
    cJSON *root = cJSON_Parse(json_start);
    cJSON *body = cJSON_GetObjectItem(root,"showapi_res_body");
    cJSON *f1 = cJSON_GetObjectItem(body,"f1");
    cJSON *weather = cJSON_GetObjectItem(f1,"day_weather_code");
    cJSON *temp_max = cJSON_GetObjectItem(f1,"day_air_temperature");
    cJSON *temp_min = cJSON_GetObjectItem(f1,"night_air_temperature");
    cJSON *wind = cJSON_GetObjectItem(f1,"day_wind_direction");

    lvgl_weather_set(weather,obj);
    lvgl_weather_temp_set(temp_max,temp_min,obj);
    lvgl_weather_wind_set(wind,obj);

    cJSON_Delete(root);

    free(buf_weather_local);
    buf_weather_local = NULL;
}

//设置天气数据
void weather_set_data(char *code,char *str,lv_img_dsc_t **img)
{
    if(!strcmp(code,"00")) {strcpy(str,"Sunny"); *img = &sun;}
    else if(!strcmp(code,"01")) {strcpy(str,"Cloudy"); *img = &cloudy;}
    else if(!strcmp(code,"02")) {strcpy(str,"Overcast"); *img = &overcast;}
    else if(!strcmp(code,"03")) {strcpy(str,"Shower"); *img = &shower;}
    else if(!strcmp(code,"04")) {strcpy(str,"T-shower"); *img = &t_shower;}
    else if(!strcmp(code,"05")) {strcpy(str,"TS-hail"); *img = &ts_hail;}
    else if(!strcmp(code,"06")) {strcpy(str,"Sleet"); *img = &sleet;}
    else if(!strcmp(code,"07")) {strcpy(str,"L-rain"); *img = &l_rain;}
    else if(!strcmp(code,"08")) {strcpy(str,"M-rain"); *img = &m_rain;}
    else if(!strcmp(code,"09")) {strcpy(str,"H-rain"); *img = &h_rain;}
    else if(!strcmp(code,"10")) {strcpy(str,"Rainstrom"); *img = &rainstrom;}
    else if(!strcmp(code,"11")) {strcpy(str,"Downpour"); *img = &downpour;}
    else if(!strcmp(code,"12")) {strcpy(str,"EX-rainstorm"); *img = &ex_rainstorm;}
    else if(!strcmp(code,"13")) {strcpy(str,"SnowShower"); *img = &snowshower;}
    else if(!strcmp(code,"14")) {strcpy(str,"L-snow"); *img = &l_snow;}
    else if(!strcmp(code,"15")) {strcpy(str,"M-snow"); *img = &m_snow;}
    else if(!strcmp(code,"16")) {strcpy(str,"H-snow"); *img = &h_snow;}
    else if(!strcmp(code,"17")) {strcpy(str,"SnowStorm"); *img = &snowstorm;}
    else if(!strcmp(code,"18")) {strcpy(str,"Foggy"); *img = &foggy;}
    else if(!strcmp(code,"19")) {strcpy(str,"FreezingRain"); *img = &freezingrain;}
    else if(!strcmp(code,"20")) {strcpy(str,"Sandstorm"); *img = &sandstorm;}
    else if(!strcmp(code,"21")) {strcpy(str,"Rainy"); *img = &rainy;}
    else if(!strcmp(code,"22")) {strcpy(str,"Rainy"); *img = &rainy;}
    else if(!strcmp(code,"23")) {strcpy(str,"Rainy"); *img = &rainy;}
    else if(!strcmp(code,"24")) {strcpy(str,"Downpour"); *img = &downpour;}
    else if(!strcmp(code,"25")) {strcpy(str,"EX-rainstorm"); *img = &ex_rainstorm;}
    else if(!strcmp(code,"26")) {strcpy(str,"M-snow"); *img = &m_snow;}
    else if(!strcmp(code,"27")) {strcpy(str,"H-snow"); *img = &h_snow;}
    else if(!strcmp(code,"28")) {strcpy(str,"SnowStorm"); *img = &snowstorm;}
    else if(!strcmp(code,"29")) {strcpy(str,"Dusty"); *img = &dusty;}
    else if(!strcmp(code,"30")) {strcpy(str,"BlowSand"); *img = &blowsand;}
    else if(!strcmp(code,"31")) {strcpy(str,"SandStrom"); *img = &sandstorm;}
    else if(!strcmp(code,"53")) {strcpy(str,"Haze"); *img = &haze;}
    else if(!strcmp(code,"301")) {strcpy(str,"Rainy"); *img = &rainy;}
    else if(!strcmp(code,"302")) {strcpy(str,"Snowy"); *img = &snowy;}
    else if(!strcmp(code,"undefined")) {strcpy(str,"N/A"); *img = &na;}
    else {strcpy(str,"N/A"); *img = &na;}
}

//设置风向字符串
void wind_set_str(char *wind_dir,char *str)
{
    if(!strcmp(wind_dir,"东风")) {strcpy(str,"East");}
    else if(!strcmp(wind_dir,"南风")) {strcpy(str,"South");}
    else if(!strcmp(wind_dir,"西风")) {strcpy(str,"West");}
    else if(!strcmp(wind_dir,"北风")) {strcpy(str,"North");}
    else if(!strcmp(wind_dir,"东南风")) {strcpy(str,"South East");}
    else if(!strcmp(wind_dir,"东北风")) {strcpy(str,"North East");}
    else if(!strcmp(wind_dir,"西南风")) {strcpy(str,"South West");}
    else if(!strcmp(wind_dir,"西北风")) {strcpy(str,"North West");}
    else if(!strcmp(wind_dir,"无持续风向")) {strcpy(str,"None");}
}

//设置风向显示
void lvgl_weather_wind_set(cJSON *wind,lv_obj_t *obj)
{
    char wind_dir[16] = {0};
    char wind_str[16] = {0};

    strcpy(wind_dir,wind->valuestring);

    wind_set_str(wind_dir,wind_str);

    char text_wind[64] = {0};
    sprintf(text_wind,"Wind: %s",wind_str);

    label_weather_wind = lv_label_create(obj);
    lv_label_set_text(label_weather_wind,text_wind);
    lv_obj_set_style_text_font(label_weather_wind,&lv_font_montserrat_20,0);
    lv_obj_set_style_text_color(label_weather_wind,lv_color_hex(0xffffff),0);
    lv_obj_align(label_weather_wind,LV_ALIGN_BOTTOM_MID,0,-15);
}

//设置温度显示
void lvgl_weather_temp_set(cJSON *temp_max,cJSON *temp_min,lv_obj_t *obj)
{
    char max[8] = {0};
    char min[8] = {0};

    strcpy(max,temp_max->valuestring);
    strcpy(min,temp_min->valuestring);
    
    char text_temp[16] = {0};

    sprintf(text_temp,"%s - %s°C",min,max);

    label_weather_temp = lv_label_create(obj);
    lv_label_set_text(label_weather_temp,text_temp);
    lv_obj_set_style_text_font(label_weather_temp,&lv_font_montserrat_20,0);
    lv_obj_set_style_text_color(label_weather_temp,lv_color_hex(0xffffff),0);
    lv_obj_align_to(label_weather_temp,img_weather,LV_ALIGN_OUT_RIGHT_MID,0,0);
}

//设置天气显示
void lvgl_weather_set(cJSON *weather,lv_obj_t *obj)
{
    char code_weather[16] = {0};
    strcpy(code_weather,weather->valuestring);
    char text_weather[32] = {0};
    lv_img_dsc_t *img;
    weather_set_data(code_weather,text_weather,&img);

    img_weather = lv_img_create(obj);
    lv_obj_align(img_weather,LV_ALIGN_LEFT_MID,25,0);
    lv_img_set_src(img_weather,img);

    label_weather = lv_label_create(obj);
    lv_obj_set_style_text_font(label_weather,&lv_font_montserrat_16,0);
    lv_obj_set_style_text_color(label_weather,lv_color_hex(0xffffff),0);
    lv_obj_align_to(label_weather,img_weather,LV_ALIGN_OUT_RIGHT_TOP,0,0);
    lv_label_set_text(label_weather,text_weather);
}

//天气
void lvgl_layout_weather(lv_obj_t *obj)
{
    lv_obj_set_size(obj,250,200);
    lv_obj_align(obj,LV_ALIGN_TOP_LEFT,0,0);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);

    lv_img_t *img_locator = lv_img_create(obj);
    LV_IMG_DECLARE(locator);
    lv_img_set_src(img_locator,&locator);
    lv_img_set_size_mode(img_locator,LV_IMG_SIZE_MODE_VIRTUAL);
    lv_img_set_zoom(img_locator,192);
    lv_obj_align(img_locator,LV_ALIGN_TOP_LEFT,-5,-5);

    label_locator = lv_label_create(obj);
    lv_obj_align_to(label_locator,img_locator,LV_ALIGN_OUT_RIGHT_MID,0,0);
    lv_label_set_text(label_locator,"Guangzhou");
    lv_obj_set_style_text_color(label_locator,lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font(label_locator,&lv_font_montserrat_20,0);

    lvgl_weather_init(obj);
}

//初始化未来天气模块
void lvgl_weather_fut_init(lv_obj_t *obj)
{
    char *request = "[request content]";

    int ret = send(fd_weather_socket,request,strlen(request),0);
    printf("weather fut send ret = %d\n",ret);
    
    ssize_t size_r = 1024 * 1024;
    buf_weather_fut = malloc(size_r);
    memset(buf_weather_fut,0,size_r);

    ret = recv(fd_weather_socket,buf_weather_fut,size_r,0);
    printf("weather fut recv ret = %d\n",ret);
    
    printf("weather fut recv %s\n",buf_weather_fut);

    char *json_start = strchr(buf_weather_fut,'{');

    cJSON *root = cJSON_Parse(json_start);
    cJSON *body = cJSON_GetObjectItem(root,"showapi_res_body");
    cJSON *dayList = cJSON_GetObjectItem(body,"dayList");

    weather_fut_set_data(dayList,obj);

    cJSON_Delete(root);

    free(buf_weather_fut);
    buf_weather_fut = NULL;
}

//设置未来天气数据
void weather_fut_set_data(cJSON *list,lv_obj_t *obj)
{
    int len_list = cJSON_GetArraySize(list);
    for(int i = 1; i < len_list; i++)
    {
        cJSON *info = cJSON_GetArrayItem(list,i);
        cJSON *date = cJSON_GetObjectItem(info,"daytime");
        cJSON *code = cJSON_GetObjectItem(info,"day_weather_code");
        cJSON *temp_max = cJSON_GetObjectItem(info,"day_air_temperature");
        cJSON *temp_min = cJSON_GetObjectItem(info,"night_air_temperature");

        lv_obj_t *layout_fut = lv_obj_create(obj);
        lv_obj_set_style_bg_opa(layout_fut,50,0);
        lv_obj_set_size(layout_fut,300,46);
        lv_obj_set_style_border_width(layout_fut,0,0);
        lv_obj_clear_flag(layout_fut,LV_OBJ_FLAG_SCROLLABLE);
        
        char date_raw[16] = {0};
        strcpy(date_raw,date->valuestring);

        int year = 2000 + (date_raw[2] - '0') * 10 + (date_raw[3] - '0');
        int month = (date_raw[4] - '0') * 10 + (date_raw[5] - '0');
        int day = (date_raw[6] - '0') * 10 + (date_raw[7] - '0');

        printf("%d %d %d\n",year,month,day);

        char text_date[16] = {0};
        sprintf(text_date,"%d/%02d/%02d",year,month,day);

        lv_label_t *label_fut = lv_label_create(layout_fut);
        lv_label_set_text(label_fut,text_date);
        lv_obj_align(label_fut,LV_ALIGN_LEFT_MID,-10,0);
        lv_obj_set_style_text_color(label_fut,lv_color_hex(0xffffff),0);

        char text_weather[16] = {0};
        lv_img_dsc_t *img;

        weather_set_data(code->valuestring,text_weather,&img);

        lv_img_t *img_fut = lv_img_create(layout_fut);
        lv_img_set_src(img_fut,img);
        lv_img_set_size_mode(img_fut,LV_IMG_SIZE_MODE_VIRTUAL);
        lv_img_set_zoom(img_fut,64);
        lv_obj_align(img_fut,LV_ALIGN_TOP_MID,-40,-52);

        label_fut = lv_label_create(layout_fut);
        lv_label_set_text(label_fut,text_weather);
        lv_obj_align(label_fut,LV_ALIGN_CENTER,25,0);
        lv_obj_set_style_text_color(label_fut,lv_color_hex(0xffffff),0);

        char temp_max_raw[8] = {0};
        char temp_min_raw[8] = {0};

        strcpy(temp_max_raw,temp_max->valuestring);
        strcpy(temp_min_raw,temp_min->valuestring);

        char text_temp[16] = {0};
        sprintf(text_temp,"%s - %s°C",temp_min_raw,temp_max_raw);

        label_fut = lv_label_create(layout_fut);
        lv_label_set_text(label_fut,text_temp);
        lv_obj_align(label_fut,LV_ALIGN_RIGHT_MID,10,0);
        lv_obj_set_style_text_color(label_fut,lv_color_hex(0xffffff),0);
    }
}

//未来七日天气
void lvgl_layout_weather_fut(lv_obj_t *obj)
{
    lv_obj_set_size(obj,325,250);
    lv_obj_align(obj,LV_ALIGN_BOTTOM_LEFT,25,-25);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_opa(obj,25,0);

    lv_label_t *label_fut = lv_label_create(obj);
    lv_obj_align(label_fut,LV_ALIGN_TOP_MID,0,3);
    lv_label_set_text(label_fut,"Weather for next 2 weeks");
    lv_obj_set_style_text_font(label_fut,&lv_font_montserrat_16,0);
    lv_obj_set_style_text_color(label_fut,lv_color_hex(0xffffff),0);

    lv_obj_t *layout_list = lv_obj_create(obj);
    lv_obj_align(layout_list,LV_ALIGN_BOTTOM_MID,0,0);
    lv_obj_set_size(layout_list,325,225);
    lv_obj_set_style_pad_all(layout_list,0,0);
    lv_obj_set_style_pad_bottom(layout_list,15,0);
    lv_obj_set_style_border_width(layout_list,0,0);
    lv_obj_set_flex_flow(layout_list,LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(layout_list,LV_FLEX_ALIGN_START,LV_FLEX_ALIGN_START,LV_FLEX_ALIGN_CENTER);
    lv_obj_set_scrollbar_mode(layout_list,LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_bg_opa(layout_list,0,0);

    lvgl_weather_fut_init(layout_list);
}
